package groupd

import (
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/ddd"
)

const (
	// DomainID 领域唯一标志
	DomainID = "group"
)

// DomainOption 域构建参数
type DomainOption struct {
	GroupRepo        GroupRepo
	GroupServiceRepo GroupRepo
}

// NewDomain 核心子域内容信息域
func NewDomain(p ddd.Domain, opt DomainOption) GroupDomain {
	d := &groupDomain{}
	d.Init(d, p, DomainID)
	initGroup(d, opt.GroupRepo)
	iniService(d, opt.GroupServiceRepo)
	return d
}

// GroupDomain 信息组域
type GroupDomain interface {
	ddd.Domain
	GroupAggregateRoot() GroupAggregateRoot
	GroupService() GroupService
}
type groupDomain struct {
	ddd.BaseDomain
	columnAggregateRoot GroupAggregateRoot
	groupService        GroupService
}

func (i *groupDomain) GroupAggregateRoot() GroupAggregateRoot {
	if i.columnAggregateRoot != nil {
		return i.columnAggregateRoot
	}
	r := i.BaseDomain.AggregateRootByID(DomainID)
	if r == nil {
		panic("没有初始化GroupAggregateRoot")
	}
	ir, ok := r.(GroupAggregateRoot)
	if !ok {
		panic("初始化GroupAggregateRoot，类型错误")
	}
	i.columnAggregateRoot = ir
	return ir
}
func (i *groupDomain) GroupService() GroupService {
	if i.groupService != nil {
		return i.groupService
	}
	r := i.BaseDomain.ServiceByID(DomainID)
	if r == nil {
		panic("没有初始化GroupService")
	}
	ir, ok := r.(GroupService)
	if !ok {
		panic("初始化GroupService，类型错误")
	}
	i.groupService = ir
	return ir
}
